﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoRel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CoreTest {
    [TestClass]
    public class KeyComparisonAndEquality : SchemaBaseData {

        [TestMethod]
        public void KeyComparer() { 
            List< KeyPairs > list = GetData();

            bool success = true;
            foreach( KeyPairs pair in list ) {

                Console.WriteLine(pair.Description);
                Console.WriteLine(pair.LHS);
                Console.WriteLine(pair.RHS);

                Assert.IsTrue(
                    pair.LHS.CompareTo( pair.RHS ) == pair.CorrectCompareResult,
                    pair.Description );
                
                Console.WriteLine();
            }
        }

        [TestMethod]
        public void EqualityOperator()
        {
            List<KeyPairs> list = GetData();

            foreach (KeyPairs pair in list) {
                Key LHS = pair.LHS;
                Key RHS = pair.RHS;

                Console.WriteLine(pair.Description);
                Console.WriteLine(LHS);
                Console.WriteLine(RHS);

                Assert.IsTrue( ( LHS == RHS ) == pair.CorrectEqualityResult );

                Console.WriteLine();
            }
        }

        [TestMethod]
        public void HashSetMembership()
        {
            List<KeyPairs> list = GetData();

            foreach (KeyPairs pair in list)
            {
                Key LHS = pair.LHS;
                Key RHS = pair.RHS;

                Console.WriteLine(pair.Description);
                Console.WriteLine(LHS);
                Console.WriteLine(RHS);


                HashSet<Key> hash = new HashSet< Key >();
                hash.Add( LHS );
                
                Assert.IsTrue( ( hash.Contains( RHS ) ) == pair.CorrectEqualityResult );

                Console.WriteLine();
            }
        }

        ///////////////////////////
        /// THESE TESTS WERE REPLACED WITH THE KeyPairs LIST
        ///////////////////////////
        

        //[TestMethod]
        //public void KeyComparisonEqual() {
        //    Schema schema = GetSchema();

        //    Key lhs = new Key( schema, new[] { "IBM", "Jim", ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", "Jim", ( IComparable ) 100, } );

        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( 0 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet< Key >();
        //    set.Add( lhs );
            
        //    Assert.IsTrue( set.Contains( rhs ), "HashSet failure." );
        //    Assert.IsTrue(lhs == rhs, "Equality operator failure");

        //}

        //[TestMethod]
        //public void KeyComparisonNotEqual() {
        //    Schema schema = GetSchema();
        //    Key lhs = new Key( schema, new[] { "IBM", "Jim", ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", "Jim", ( IComparable ) 101, } );
        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( -1 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet<Key>();
        //    set.Add(lhs);

        //    Assert.IsFalse(set.Contains(rhs), "HashSet failure.");
        //    Assert.IsFalse(lhs == rhs, "Equality operator failure");

        //}


        //[TestMethod]
        //public void KeyComparisonEqualWithNulls() {
        //    Schema schema = GetSchema();
        //    Key lhs = new Key( schema, new[] { "IBM", ( IComparable ) null, ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", ( IComparable ) null, ( IComparable ) 100, } );
        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( 0 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet<Key>();
        //    set.Add(lhs);

        //    Assert.IsTrue(set.Contains(rhs), "HashSet failure.");
        //    Assert.IsTrue(lhs == rhs, "Equality operator failure");
        //}

        //[TestMethod]
        //public void KeyComparisonNotEqualWithNulls() {
        //    Schema schema = GetSchema();

        //    Key lhs = new Key( schema, new[] { "IBM", ( IComparable ) null, ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", ( IComparable ) null, ( IComparable ) 101, } );
        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( -1 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet<Key>();
        //    set.Add(lhs);

        //    Assert.IsFalse(set.Contains(rhs), "HashSet failure.");
        //    Assert.IsFalse(lhs == rhs, "Equality operator failure");
        //}

        //[TestMethod]
        //public void KeyComparisonNotEqualWithNullOnlyRHS() {
        //    Schema schema = GetSchema();

        //    Key lhs = new Key( schema, new[] { "IBM", ( IComparable ) null , ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", ( IComparable ) "Sam", ( IComparable ) 100, } );
        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( -1 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet<Key>();
        //    set.Add(lhs);

        //    Assert.IsFalse(set.Contains(rhs), "HashSet failure.");
        //    Assert.IsFalse(lhs == rhs, "Equality operator failure");
        //}

        //[TestMethod]
        //public void KeyComparisonNotEqualWithNullOnlyLHS() {
        //    Schema schema = GetSchema();
        //    Key lhs = new Key( schema, new[] { "IBM", ( IComparable ) "Sam", ( IComparable ) 100, } );
        //    Key rhs = new Key( schema, new[] { "IBM", ( IComparable ) null , ( IComparable ) 100, } );
        //    Console.WriteLine( lhs );
        //    Console.WriteLine( rhs );
        //    int comparison_result = lhs.CompareTo( rhs );
        //    Console.WriteLine( "Compare Result:"
        //                       + comparison_result );

        //    Assert.IsTrue( 1 == comparison_result, "Failed to properly compare equal keys" );

        //    HashSet<Key> set = new HashSet<Key>();
        //    set.Add(lhs);

        //    Assert.IsFalse(set.Contains(rhs), "HashSet failure.");
        //    Assert.IsFalse( lhs == rhs, "Equality operator failure" );
        //}
    }
}